package com.bestcem.xm.common.core.exception;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
import com.bestcem.xm.common.core.domain.web.XmResultJson;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.support.DefaultMessageSourceResolvable;
import org.springframework.validation.BindException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.NoHandlerFoundException;

import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;
import java.util.stream.Collectors;

@ResponseBody
@ControllerAdvice(basePackages = "com.bestcem.xm")
@Slf4j
public class GlobalExceptionHandler {

    @ExceptionHandler({MethodArgumentNotValidException.class})
    public XmResultJson<String> processException(MethodArgumentNotValidException e) {
        String message = e.getBindingResult().getAllErrors().stream().map(DefaultMessageSourceResolvable::getDefaultMessage).collect(Collectors.joining(";"));
        return XmResultJson.fail(-1, message);
    }

    @ExceptionHandler({ConstraintViolationException.class})
    public XmResultJson<String> processException(ConstraintViolationException e) {
        String messages = e.getConstraintViolations().stream().map(ConstraintViolation::getMessage).collect(Collectors.joining(";"));
        return XmResultJson.fail(-1, messages);
    }

    @ExceptionHandler({UnauthorizedException.class})
    public XmResultJson<String> processException(UnauthorizedException e) {
        return XmResultJson.fail(-1, "unauthorized");
    }

    @ExceptionHandler({MybatisPlusException.class})
    public XmResultJson<String> processException(MybatisPlusException e) {
        log.error("MybatisPlusException", e);
        return XmResultJson.fail(-1, "系统异常");
    }

    @ExceptionHandler({BusinessException.class})
    public XmResultJson<String> processException(BusinessException e) {
        log.error("{BusinessException}", e);
        return XmResultJson.fail(StrUtil.isNotEmpty(e.getCode()) ? Integer.parseInt(e.getCode()) : -1, e.getMessage());
    }

    @ExceptionHandler({BindException.class})
    public XmResultJson<String> processException(BindException e) {
        String message = e.getBindingResult().getAllErrors().stream().map(DefaultMessageSourceResolvable::getDefaultMessage).collect(Collectors.joining(";"));
        return XmResultJson.fail(-1, message);
    }

    @ExceptionHandler({HttpRequestMethodNotSupportedException.class})
    public XmResultJson<String> processException(HttpRequestMethodNotSupportedException e) {
        String message = e.getMessage();
        return XmResultJson.fail(-1, message);
    }

    @ExceptionHandler({NoHandlerFoundException.class})
    public XmResultJson<String> processException(NoHandlerFoundException e) {
        String message = "无效的请求地址";
        return XmResultJson.fail(-1, message);
    }

    @ExceptionHandler({MissingServletRequestParameterException.class})
    public XmResultJson<String> processException(MissingServletRequestParameterException e) {
        String message = "缺少必要的参数[" + e.getMessage() + "]";
        return XmResultJson.fail(-1, message);
    }

    @ExceptionHandler({ServiceException.class})
    public XmResultJson<String> processException(ServiceException e) {
        return XmResultJson.fail(-1, e.getMessage());
    }
}
